﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Web;
using System.IO;
using System.Threading;
using Newtonsoft.Json.Linq;
using blackhouse.BlackApart.Business.Opinions;
using System.Globalization;
using blackhouse.BlackApart.Business.PushMails;
using blackhouse.Web;
using blackhouse.BlackApart.Business.Newsletter;
using blackhouse.AsyncMail.Business;
using blackhouse.AsyncMail.Business.Data;

namespace blackhouse.Aparts.Web.Handlers {
	/// <summary>
	/// Summary description for ApartsService
	/// </summary>
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[System.ComponentModel.ToolboxItem(false)]
	[System.Web.Script.Services.ScriptService]
	public class ApartsService : System.Web.Services.WebService {

		private HttpRequest request;
		private Factory factory = Factory.Instance();

		[WebMethod]
		public void AddQuickContact(string phoneNumber) {
			QuickContactPushMail quickContact = new QuickContactPushMail(phoneNumber);
			quickContact.PushMail();
		}

		[WebMethod]
		public HandStatus AddNewsletterSubscriber(string email, string lang) {
			Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
			ApartsResources resx = new ApartsResources();

			if (!email.IsValidEmail())
				return new HandStatus(false, 1, resx.GetString("incorrectEmail"));
			ISubscriber subscriber = Factory.Instance().GetSubscriber(email);
			if (subscriber.Exist())
				return new HandStatus(false, 2, resx.GetString("emailExists"));
			try {
				if (!subscriber.Save())
					return new HandStatus(false, 3, resx.GetString("serverError"));
			}
			catch (Exception) {
				return new HandStatus(false, 3, resx.GetString("serverError"));
			}

			return new HandStatus(true, 4, resx.GetString("newsletterAdd"));
		}

        //[WebMethod]
        //public void AddOpinion() {
        //    if (HttpContext.Current == null || HttpContext.Current.Request == null) return;
        //    this.request = HttpContext.Current.Request;

        //    string json = "";
        //    if (request.ContentType.Contains("json")) {
        //        request.InputStream.Seek(0, SeekOrigin.Begin);
        //        using (StreamReader sr = new StreamReader(request.InputStream)) {
        //            json = sr.ReadToEnd();
        //        }
        //        JObject jo = JObject.Parse(json);
        //        SaveUserOpinion(jo);
        //    }
        //}

        //private void SaveUserOpinion(JObject data) {

        //    IOpinion opinion = Factory.Instance().GetOpinion();
        //    opinion.InMinusOpinion = Convert.ToString(data["InMinus"]);
        //    opinion.InPlusOpinion = Convert.ToString(data["InPlus"]);
        //    opinion.PublishedDate = DateTime.Now;
        //    opinion.Publisher = new AuthorOpinion();
        //    opinion.Publisher.Category = Convert.ToString(data["StayType"]);
        //    opinion.Publisher.Country = Convert.ToString(data["Country"]);
        //    opinion.Publisher.FirstName = Convert.ToString(data["ClientFirstName"]);
        //    opinion.Publisher.LastName = Convert.ToString(data["ClientLastName"]);

        //    for (int xod = 0; xod < data["PartOpinions"].Count(); xod++) {
        //        opinion.AddOpinionDetailValue(data["PartOpinions"][xod]["categoryId"].Value<int>(), data["PartOpinions"][xod]["value"].Value<float>());
        //    }

        //    opinion.Save();

        //    IOpinionToReservation otr = Factory.Instance().CreateOpinionToReservationTrans();
        //    otr.OpinionId = opinion.Id;
        //    otr.ReservationId = data["ReservationId"].Value<int>();
        //    otr.Save();
        //}

        //private void SetClient(IUserApartmentsReservation reservation) {
        //    string email = this.request["personEmail"];
        //    if (email.IsValidEmail()) {

        //        string firstName = "", lastName = "";
        //        string[] nameParts = this.request["personName"].Split(' ');
        //        if (nameParts.Length > 0)
        //            firstName = nameParts[0];
        //        if (nameParts.Length > 1)
        //            lastName = string.Join(" ", nameParts.Skip(1).ToArray());

        //        reservation.SetClient(email, this.request["personPhone"], firstName, lastName, "p.", blackhouse.BlackApart.Web.Modules.DisplayCurrency.ReadUserCurrencyFromCookie());
        //    }
        //}

	}
}
